package com.xudow.app.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeshare.edu.ucenter.models.user.UserProfileWithOther;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.util.ImageUtils;

import java.util.List;

/**
 * 学生选择List Adapter
 *
 * @author lukecheng
 */
public class StudentAdapter extends ArrayAdapter<UserProfileWithOther> {

    private int resource;

    public StudentAdapter(Context context, int resource, List<UserProfileWithOther> objects) {
        super(context, resource, objects);

        this.resource = resource;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (null == convertView) {
            convertView = LayoutInflater.from(getContext()).inflate(this.resource, null);

            holder = new ViewHolder();
            holder.username = (TextView) convertView.findViewById(R.id.username);
            holder.xuedouNo = (TextView) convertView.findViewById(R.id.xuedou_no);
            holder.photo = (ImageView) convertView.findViewById(R.id.photo);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        UserProfileWithOther item = getItem(position);
        holder.username.setText(item.getName());
        holder.xuedouNo.setText(item.getXuedouId());

        String imageUrl = String.format(Config.IMAGE_LOAD_URL_PARAMS, item.getPhotoPath());
        ImageUtils.loadImage(this.getContext(), holder.photo, imageUrl, R.mipmap.default_portrait);


        return convertView;
    }

    class ViewHolder {
        public TextView username, xuedouNo;
        public ImageView photo;
    }

}
